package com.wl.dzt.sys.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/6/6/006
 * @description 资源工具
 */
public class ResourceUtils {
    public static final Logger log = LoggerFactory.getLogger(ResourceUtils.class);

    /**
     * 加载类路径下资源
     *
     * @param fileName
     * @return
     */
    public static InputStream loadtContextClassPathResource(String fileName) {
        return Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
    }

    /**
     * 加载properties 文件
     *
     * @param url
     * @return
     */
    public static Map<String, String> loadProperties(String url) {
        Map<String, String> map = new HashMap<>();
        try {
            InputStream is = loadtContextClassPathResource(url);
            if (is == null) {
                log.warn("没有加载到[{}]文件......", url);
                return map;
            }
            Reader reader = new InputStreamReader(is,
                    "utf-8");
            Properties properties = new Properties();
            properties.load(reader);
            properties.entrySet().forEach(e -> map.put(e.getKey().toString(), e.getValue().toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
